#!/usr/bin/python3
# coding=utf-8

import os
import sys
from loguru import logger
from datetime import datetime
from utils.yamlUtil import YamlUtil


def get_root_path():
    all_vars = YamlUtil().data_load()
    root_path = all_vars.get("ROOT_PATH")
    try:
        if os.path.exists(root_path):
            return root_path
        else:
            raise FileNotFoundError("目标路径不存在")

    except FileNotFoundError as e:
        logger.error("项目根目录: {} 配置出现异常.".format(root_path))


def get_logger():
    root_path = get_root_path()
    log_path = os.path.join(root_path, "logs")
    file_name = "Exec_{}.log".format(datetime.now().strftime("%Y-%m-%d"))
    target_file = os.path.join(log_path, file_name)
    logger.configure(
        handlers=[
            {
                "sink": target_file,
                "level": "INFO",
                "encoding": "utf-8",
                "rotation": "100 MB",
            }
        ]
    )  # DEBUG < INFO
    # logger.add(target_file, enqueue=True, encoding='utf-8') # 异步写入文件
    logger.add(sys.stdout, level="WARNING")  # 输出到控制台
    return logger


logObj = get_logger()

if __name__ == "__main__":
    logObj = get_logger()
    logObj.info("this is info log")
    logObj.debug("this is debug log")
    logObj.warning("this is warning log")
    logObj.error("this is error log")
